<?php
/**
 * @filename NetworkQuestionAttach.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-12-3 11:07:36
 * @version 1.0
 * @Description 网点问卷--答案的附件
 */
class NetworkQuestionAttach extends CActiveRecord
{
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'network_question_attach';
    }
    /**
     * 得到网点问卷的附件
     */
    public function getByNq($nq_id)
    {
        $data = $this->findAll('nq_id='.$nq_id);
        if ($data) 
        {
            $result = array();
            foreach ($data as $key => $val) {
                $subject_id = $val['subject_id'];
                $result[$subject_id][] = $val['url'];
            }
            return $result;
        }
        return $data;
    }
    protected $attach_type = array(
        'img' => array('jpg','jpeg','gif','png','bmp'),
        'video' => array('mp4','avi','wmv','wtv','ogv','webmv','m4v','flv'),
        'audit' => array('mp3','wma','m4a','webma','wav')
    );
    protected $video_type = array(
        'mp3' => array('mp3','wma'),
        'mp4' => array('mp4'),
        'wmv' => array('wmv','avi'),
        
    );
    public function getBySubject($nq_id, $subject_id, $more=true){
        $data = $this->findAll('nq_id='.$nq_id . ' and subject_id='.$subject_id);
        if ($data&&$more) {
            foreach ($data as $key => $val) 
            {
                $info1 = pathinfo($val['url']);
                $extension = strtolower($info1['extension']);
                foreach ($this->attach_type as $k => $v) 
                {
                    if (in_array($extension, $v)) 
                    {
                        $data[$key]['extension'] = $k;
                        $data[$key]['ext'] = $extension;
                        foreach ($this->video_type as $tk => $tv) {
                            if (in_array($extension, $tv)) {
                                $data[$key]['ext'] = $tk;
                            } 
                        }
                        if ($k == 'video' || $k == 'audit') {
                            $data['have_video'] = 1;
                        }
                        
                    }
                }
            }
        }
        return $data;
    }
}